package com.lej.dataservice.club.data

import com.lej.core.base.BaseModel
import com.lej.dataservice.club.http.checkData
import com.lej.core.utils.extention.safe
import com.lej.dataservice.club.bean.commercial.CommercialBean
import com.lej.dataservice.club.http.baseApiService
import com.lej.dataservice.club.room.db
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

object CommercialRepository : BaseModel() {
    private val dao by lazy { db.commercialDao() }
    val commercialList get() = dao.validList()
    suspend fun refreshList() {
        withContext(Dispatchers.IO) {
            val list = kotlin.runCatching {
                baseApiService.liststartad().checkData()
            }.getOrNull()
            dao.deleteNotContains(list?.all.safe())
            list?.all.safe().let {
                dao.upsert(it)
            }
        }
    }

    suspend fun showTimePlus(bean: CommercialBean) {
        bean.showTime = System.currentTimeMillis()
        withContext(Dispatchers.IO) {
            dao.update(bean)
        }
    }
}